﻿/*
frame.js

The MIT License

Copyright (c) 2011 Yuya.Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// フレームの管理を行う

//! フレーム管理クラス
function EspFrame() {
	this.count = 0;
	this.enableUpdate = false;
	this.canvalElementID = "canvas";

	this.canvasClearColor = "#000000";
	this.canvasWidth = 640;
	this.canvasHeight = 480;
	this.canvas = null;
	this.context2d = null;

	this.isAbort = true;
	this.test = 0;
}

EspFrame.lastTime = new Date();

//! コンテキスト取得
EspFrame.prototype.getContext = function(){
	if(this.context2d == null){
		this.canvas = window.document.getElementById(this.canvalElementID);
		if(this.canvas){
			this.context2d = this.canvas.getContext('2d');
			if(this.context2d){
				this.setCanvasSize(this.canvasWidth, this.canvasHeight);
				// クリアカラーでクリアする
				this.context2d.fillStyle = this.canvasClearColor;
				this.context2d.fillRect(0, 0, this.canvasWidth, this.canvasHeight);
			}
		}
	}
}

EspFrame.prototype.setCanvasSize = function(width, height){
	this.canvasWidth = width;
	this.canvasHeight = height;
	if(this.canvas != null){
		this.canvas.width = width;
		this.canvas.height = height;
	}
}

EspFrame.prototype.setFullScreen = function(){
	this.setCanvasSize(window.innerWidth, window.innerHeight);
}

//! フレームの更新処理
EspFrame.prototype.update = function () {
	// 更新前デバッグフック
	if(this.enableUpdate){
	}
	// 描画を実行する
	if(this.context2d != null){
		this.context2d.fillStyle = this.canvasClearColor;
		this.context2d.fillRect(0, 0, this.canvasWidth, this.canvasHeight);
		var fontsize = 12;
		this.context2d.font = fontsize + "px 'ＭＳ Ｐゴシック'";
		this.context2d.fillStyle = "#ffffff";
		this.context2d.fillText("F:"+this.count, 5, fontsize * 1);
		this.context2d.fillText("W:"+this.canvasWidth+"H:"+this.canvasHeight, 5, fontsize * 2);
		this.context2d.fillText("色々実験中", 5, fontsize * 3);

		this.context2d.beginPath();
		this.context2d.arc(this.canvasWidth / 2, this.test, 20, 0, 360, false);
		this.context2d.strokeStyle = "#00ff00";
		this.context2d.lineWidth = 2;
		this.context2d.stroke();
		this.context2d.closePath();

		this.test += 5;
		if(this.test > this.canvasHeight){
			this.test = 0;
		}
	}
	// 経過時間を考慮して次の実行時間を設定する
	var nowTime = new Date();
	// 60FPS
	var nextTime = 16.666 - (nowTime.getTime() - EspFrame.lastTime.getTime());
	if(nextTime <= 0){
		nextTime = 1;
	}
	EspFrame.lastTime = nowTime;
	this.count++;
	if(this.isAbort == false){
		var _this = this;
		setTimeout(function(){_this.update();}, nextTime);
	}
}
//! 実行シーンを差し替える
EspFrame.prototype.changeScene = function () {
}

//! 更新を開始する
EspFrame.prototype.play = function(){
	if(this.isAbort == true){
		this.isAbort = false;
		EspFrame.lastTime = new Date();
		this.update();
	}
}

//! 更新を一時停止する
EspFrame.prototype.pause = function(){
	this.isAbort = true;
}

var espFrame = new EspFrame();


